const Service = require('egg').Service;

class VideoService extends Service {
    get Video() {
        return this.app.model.Video;
    }
    async upload(videoinfo) {
        const {title,userid,vodid} = videoinfo
        const videomodel = new this.Video({title,userid,vodid})
        const dbback = await videomodel.save()
        return dbback
    }

    async videoisexit(vodid,userid){
        return await this.Video.countDocuments({vodid,userid})
    }

    async list(page,pagesize){
        const skip = (page-1)*pagesize
        const videos = await this.Video.find().skip(skip).limit(pagesize).sort({_id:-1})
        const count = await this.Video.countDocuments()
        return {videos,count}
    }

    async getvideo(vodid){
        const dbback = await this.Video.findOne({vodid}
            ,{ _id: 1,userid:1 ,title: 1,vodid:1,create_time:1}).populate('userid','account nickname avater')
        return dbback
    }

    async videodetail(id){
        const dbback = await this.Video.findOne({_id:id})
        return dbback
    }

    async updatevideo(id,videoinfo){
        const dbback = await this.Video.findByIdAndUpdate(id,videoinfo)
        return dbback
    }
}

module.exports = VideoService;